package com.yunyi.common.utils.oss;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.VoidResult;
import com.yunyi.common.config.AliyunOssConfig;
import com.yunyi.common.utils.SecurityUtils;
import com.yunyi.common.utils.file.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * @ClassName: AliyunOssConfig
 * @Author: ZouLi
 * @Description: 阿里云对象存储工具类
 * @Date: 2023/8/28 9:14
 **/
@Component
public class AliyunOssFileUtils {

    /**
     * 配置信息
     **/
    private static AliyunOssConfig aliyunOssConfig;

    /**
     * 使用构造方法注入配置信息
     */
    @Autowired
    public AliyunOssFileUtils(AliyunOssConfig aliyunOssConfig) {
        AliyunOssFileUtils.aliyunOssConfig = aliyunOssConfig;
    }

    private static OSS ossClient;

    /**
     * @Description: 设置为单例
     * @Param []
     * @Return void
     * @Throws
     **/
    private static void createOssClient() {
        if (ossClient == null){
            ossClient = new OSSClientBuilder().build(aliyunOssConfig.getEndpoint(), aliyunOssConfig.getAccessKeyId(), aliyunOssConfig.getAccessKeySecret());
        }
    }

    /**
     * @Description: 关闭OSSClient
     * @Param []
     * @Return void
     * @Throws
     **/
    public static void shutdown(){
        if (ossClient != null){
            ossClient.shutdown();
            ossClient = null;
        }
    }
 
    /**
     * 上传文件(不指定根路径)
     * @param file
     * @return
     * @throws Exception
     */
    public static String uploadFile(MultipartFile file) throws Exception {
        return uploadFile(file, "");
    }

    /**
     * 上传文件(指定根路径)
     * @param file
     * @return
     * @throws Exception
     */
    public static String uploadFile(MultipartFile file, String basePath) throws Exception {
        // 生成 OSSClient
        createOssClient();
        // 原始文件名称
        // String originalFilename = file.getOriginalFilename();

        // 编码文件名
        String filePathName = FileUploadUtils.extractFilename(file);
        // 文件路径名称
        filePathName = aliyunOssConfig.getFilehost() + "/"+ basePath + filePathName;
        try {
            // 添加
            ossClient.putObject(aliyunOssConfig.getBucketName(), filePathName, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接
            shutdown();
        }
        return aliyunOssConfig.getUrl() + "/" + filePathName;
    }

    /**
     * @Description: 下载文件
     * @Param [objectName]
     * @Return java.io.InputStream
     * @Throws
     **/
    public static InputStream downloadFile(String objectName){
        // 生成 OSSClient
        createOssClient();

        // 根路径名
        String basePath = aliyunOssConfig.getBaseUrl();
        // 格式化文件名
        objectName = objectName.substring(objectName.lastIndexOf(basePath));

        // 读取之后的文件对象
        OSSObject object = null;
        try {
            // 读取
            object = ossClient.getObject(aliyunOssConfig.getBucketName(), objectName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object.getObjectContent();
    }

    /**
     * @Description: 删除文件
     * @Param [objectName]
     * @Return boolean
     * @Throws
     **/
    public static boolean deleteFile(String objectName) {
        // 生成 OSSClient
        createOssClient();

        // 根路径名
        String basePath = aliyunOssConfig.getBaseUrl();
        // 格式化文件名
        objectName = objectName.substring(objectName.lastIndexOf(basePath));

        VoidResult result = null;
        try {
            // 获取状态码
            result = ossClient.deleteObject(aliyunOssConfig.getBucketName(), objectName);
            if (result.getResponse().getStatusCode() == 200) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭连接
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return false;
    }

}
